package com.lujieni.msg.provider.mp;

import com.lujieni.msg.common.properties.rabbitmq.CommonMqQueueProperties;
import com.lujieni.msg.common.properties.rabbitmq.RabbitMqQueueMapProperties;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author
 * @Title: ExchangeAndQueueConfig
 * @Description:
 * @date 2022/6/16 9:27 下午
 *
 * 一个很普通的交换机到队列的方式
 * createTraceExchange=>createTraceQueue
 */
@Configuration
@EnableConfigurationProperties(RabbitMqQueueMapProperties.class)
public class LocalEventMqConfig {

    @Autowired
    private RabbitMqQueueMapProperties rabbitMqQueueMapProperties;

    @Bean
    public DirectExchange createLocalEventExchange(){
        CommonMqQueueProperties commonMqQueueProperties = rabbitMqQueueMapProperties.getQueueMap().get("local-event");
        return new DirectExchange(commonMqQueueProperties.getExchangeName(), true, false);
    }

    @Bean
    public Queue createLocalEventQueue(){
        CommonMqQueueProperties commonMqQueueProperties = rabbitMqQueueMapProperties.getQueueMap().get("local-event");
        return QueueBuilder.durable(commonMqQueueProperties.getQueueName())
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", commonMqQueueProperties.getDeadQueueName())
                .build();
    }

    @Bean
    public Binding createTraceBinding(){
        CommonMqQueueProperties commonMqQueueProperties = rabbitMqQueueMapProperties.getQueueMap().get("local-event");
        return BindingBuilder.bind(createLocalEventQueue()).to(createLocalEventExchange()).with(commonMqQueueProperties.getRoutingKey());
    }

    @Bean
    public Queue createLocalEventDeadQueue(){
        CommonMqQueueProperties commonMqQueueProperties = rabbitMqQueueMapProperties.getQueueMap().get("local-event");
        return new Queue(commonMqQueueProperties.getDeadQueueName(), true);
    }

}
